<?php

namespace app\admin\controller\service_config;

use app\common\model\admin\service_config\ServiceConfigTab;
use think\admin\Controller;
use think\admin\helper\QueryHelper;

/**
 *  系统服务配置分组
 * @class ConfigTab
 * @package app\admin\controller\service_config
 * @Author: shirukun
 * @Date: 2024/11/7 16:53
 */
class ConfigTab extends Controller
{

    /**
     * 默认检测登陆后台用户
     *
     * @var bool
     */
    public $check_user = false;

    /**
     * 列表查询自动构造公司id条件
     * @var array
     */
    public $checkCompany = false;

    /**
     *  配置分组管理
     * @auth true
     * @menu true
     */
    public function index()
    {
        ServiceConfigTab::mQuery()->layTable(function () {
            $this->title = lang('配置分组');
//            $this->types = SystemBase::mk()->where(['deleted' => 0])->where(['type' => '配置分类'])->distinct()->column('name,code');
            // $this->types = ServiceConfigTab::$types;
            $this->types = array_map('lang', ServiceConfigTab::$types);

            $this->type = $this->get['type'] ??  key($this->types);

        }, function (QueryHelper $query) {
            $query->order('sort desc');
            // 数据列表搜索过滤
            $query->equal('code,type,status')->like('name#name');
        });
    }

    protected function _index_page_filter(&$data)
    {
        $services = [];
        foreach (ServiceConfigTab::$services as $key => $value) {
            $services[$key] = array_map('lang', $value);
        }

        foreach ($data as &$v){
            $v['service'] = $services[$v['type']][$v['service']] ?? '-';
        }
    }

    /**
     * 添加配置分组
     * @auth true
     */
    public function add()
    {
//        $this->_applyFormToken();
        ServiceConfigTab::mForm('form');
    }

    /**
     * 编辑配置分组
     * @auth true
     */
    public function edit()
    {
//        $this->_applyFormToken();
        ServiceConfigTab::mForm('form');
    }

    /**
     * 修改数据状态
     * @auth true
     */
    public function state()
    {
        ServiceConfigTab::mSave($this->_vali([
            'status.in:1,0'  => lang('状态值范围异常'),
            'status.require' => lang('状态值不能为空'),
        ]));
    }

    /**
     * 删除配置分组
     * @auth true
     */
    public function remove()
    {
        ServiceConfigTab::mDelete();
    }

    /**
     * 表单数据处理
     * @param array $vo
     * @throws \ReflectionException
     */
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isGet()) {
            $this->types = array_map('lang', ServiceConfigTab::$types);
            $this->type = $this->get['type'];
            $this->services = array_map('lang', ServiceConfigTab::$services[$this->type]);
        }else{
            $param = $this->request->param();
            if (!isset($param['id'])){
                $res = ServiceConfigTab::mk()->where(['type' => $param['type'],'code' => $param['code']])->find();
                if ($res){
                    return $this->error(lang('此分组已存在'));
                }
            }
        }
    }
}